package com.bytz.modules.cms.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.product.constant.ProductConstants;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.mapper.ProductMapper;
import com.bytz.modules.cms.product.service.IProductService;
import com.bytz.modules.cms.reseller.service.IResellerLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 莱宝CMS—产品管理—产品基本信息表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    protected IResellerLevelService resellerLevelService;

    @Override
    public Product getByProductNumber(String productNumber) {
        return lambdaQuery()
                .eq(Product::getProductNumber, productNumber)
                .one();
    }

    @Override
    public List<Product> getByProductNumbers(Collection<String> productNumber) {

        if (CollectionUtil.isEmpty(productNumber)) {
            return new ArrayList<>();
        }

        return lambdaQuery()
                .in(Product::getProductNumber, productNumber)
                .list();
    }

    @Override
    public List<Product> getByIds(Collection<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }

        return lambdaQuery()
                .in(Product::getId, ids)
                .list();
    }

    @Override
    public Boolean setIsEc(Collection<String> productNumbers, boolean isEc) {

        if (CollectionUtil.isEmpty(productNumbers)) {
            return true;
        }

        return lambdaUpdate()
                .in(Product::getProductNumber, productNumbers)
                .set(Product::getIsEc, isEc)
                .update();
    }

    @Override
    public List<Product> getEcProducts() {
        return lambdaQuery()
                .eq(Product::getIsEc, true)
                .list();
    }

    @Override
    public List<Product> searchProduct(String productNumber) {

        // 最少3个字母再查询
        if (productNumber == null || productNumber.length() < 3) {
            return null;
        }

        return lambdaQuery()
                .select(Product::getProductNumber,
                        Product::getMaterialDesc,
                        Product::getStatus,
                        Product::getReplaceProductNumber)
                .likeRight(Product::getProductNumber, productNumber)
                .eq(Product::getStatus, ProductConstants.ProductStatus.ENABLE)
                .last("LIMIT 10")
                .list();
    }

    @Override
    public List<Product> hasHierarchyExclude(List<String> hierarchyNumbers) {
        if (CollectionUtil.isEmpty(hierarchyNumbers)) {
            throw new BytzBootException("hierarchyNumbers为空");
        }
        return lambdaQuery()
                .select(Product::getProductNumber, Product::getHierarchyNumber)
                .notIn(Product::getHierarchyNumber, hierarchyNumbers)
                .list();
    }
}
